<?php

/**
 * @Author:      zk
 * @DateTime:    18-4-10 下午1:30
 * @Description: 用于维护 cms 耗材 的维护和管理     [同步 datamodel下的 DataSupplyItem]
 *
 */

class SupplyItem extends Eloquent
{

    protected $table = "supply_item";

    public static $types = [
        0=>'标准',
        1=>'非标',
    ];
    public function series()
    {
        return $this->hasOne('DataSupplySeries',"id","series_id");
    }

    public function brand()
    {
        return $this->hasOne('DataSupplyBrand',"id","brand_id");
    }
    public function unit()
    {
        return $this->hasOne('DataSupplyUnit',"id","unit_id");
    }
    public function saleUnit()
    {
        return $this->hasOne('DataSupplyUnit',"id","sale_unit_id");
    }
    public function created_man()
    {
        return $this->hasOne('SystemUser',"id","created_by");
    }
    public function updated_man()
    {
        return $this->hasOne('SystemUser',"id","updated_by");
    }


    /**
    * @Author:      lyt
    * @DateTime:    2016-04-19 17:59:36
    * @Description: 车检索条件 车条件类型 检索条件获取 符合条件的耗材id
    */
    public static  function retrievalCode($car_retrieval_code,$type,$retrieval_code,$car_str="",$level_type=0,$level_code=0){
        $si=SupplyItem::orderBy("supply_item.created_at","desc");
        // ;
        if($car_retrieval_code&&$type){//如果存在 检索条件和类型
            $si=$si->leftJoin("anhui_data.supply_product_item","anhui_data.supply_product_item.supply_item_id","=","supply_item.id")
                ->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","supply_product_item.car_model_id")
            ->where("supply_product_item.status",1);
            switch ($type) {
                case 1://如果是品牌
                    $si=$si->where("car_standard_info.brand_name_id",$car_retrieval_code);
                    break;
                case 2://如果是系列
                    $si=$si->where("car_standard_info.series_name_id",$car_retrieval_code);
                    break;
                case 3://如果是年款
                    $c_arr=explode(',', $car_retrieval_code);
                    if(isset($c_arr[0])){
                        $si=$si->where("car_standard_info.series_name_id",$c_arr[0]);
                        if(isset($c_arr[1])){
                            $si=$si->where("car_standard_info.model_year",$c_arr[1]);
                        }
                    }
                    break;
                case 4://如果是型号
                    $si=$si->where("car_standard_info.id",$car_retrieval_code);
                    break;
            }
        }

        if($level_type!=0&&$level_code!=0){//如果存在 三级分类检索天剑
            $si=$si->leftJoIn("supply_classification as sc3","sc3.id","=","supply_item.supply_classification_id")
            ->leftJoIn("supply_classification as sc2","sc2.id","=","sc3.parent_id")
            ->leftJoIn("supply_classification as sc1","sc1.id","=","sc2.parent_id");
            switch ($level_type) {
                case 1://如果是一级
                    $si=$si->where("sc1.id",$level_code);
                    break;
                case 2://如果二级
                    $si=$si->where("sc2.id",$level_code);
                    break;
                case 3://如果三级
                    $si=$si->where("sc3.id",$level_code);
                    break;
            }
        }
        if($retrieval_code){
            $si=$si->where("supply_item.retrieval_code","like","%".$retrieval_code."%");
            // $result=Scws::participle($retrieval_code);
            // Log::info($result);
            // if($result["code"]==0){//说明分词成功    
            //     foreach ($result["result"] as $re) {
            //         $si=$si->where("supply_item.retrieval_code","like","%".$re."%");
            //     }
            // }else{//说明分词失败
            //     $si=$si->where("supply_item.retrieval_code","like","%".$retrieval_code."%");
            // }            
        }
        if($car_str){
            $si=$si->where("supply_item.car_model_comments","like","%".$car_str."%");                        
        }
        $si=$si->select("supply_item.id")->take(50000)->get();
        $temp_id=array();
        foreach ($si as $_s) {
            $temp_id[]=$_s->id;
        }
        return array_unique($temp_id);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-11 14:12:06
     * @Description: 检索条件获取 符合条件的耗材id
     */
    public static  function retrievalCodeNew($retrieval_code,$car_str="",$level_type=0,$level_code=0,$three){
        $si=SupplyItem::orderBy("supply_item.id","desc");
        if($level_type!=0&&$level_code!=0){//如果存在 三级分类检索条件
            $si=$si->leftJoIn("supply_classification as sc3","sc3.id","=","supply_item.supply_classification_id")
            ->leftJoIn("supply_classification as sc2","sc2.id","=","sc3.parent_id")
            ->leftJoIn("supply_classification as sc1","sc1.id","=","sc2.parent_id");
            switch ($level_type) {
                case 1://如果是一级
                    $si=$si->where("sc1.id",$level_code);
                    break;
                case 2://如果二级
                    $si=$si->where("sc2.id",$level_code);
                    break;
                case 3://如果三级
                    $si=$si->where("sc3.id",$level_code);
                    break;
            }
        }
        if($retrieval_code){
            // $result=Scws::participle($retrieval_code);
            // Log::info($result);
            // if($result["code"]==0){//说明分词成功    
            //     foreach ($result["result"] as $re) {
            //         $si=$si->where("supply_item.retrieval_code","like","%".$re."%");
            //     }
            // }else{//说明分词失败
            //     $si=$si->where("supply_item.retrieval_code","like","%".$retrieval_code."%");
            // }    
            $si=$si->where("supply_item.retrieval_code","like","%".$retrieval_code."%");
        }
        if($car_str){
            $si=$si->where("supply_item.car_model_comments","like","%".$car_str."%");                        
        }
        if(sizeof($three)>0){
            $si=$si->whereIn('supply_item.supply_classification_id',$three);
        }
        $si=$si->select("supply_item.id");
        $temp_id=array();
        $si->chunk(10000, function($si_arr) use (&$temp_id) {                    
            foreach ($si_arr as $_s) {
                $temp_id[]=$_s->id;
            }
        });
        unset($si);
        
        return $temp_id;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-04-22 10:34:18
     * @Description: 根据 id 获得item的 详细信息
     */
    public static function getCacheItemInfo($item_id){
        if(Cache::has('supply_item_'.$item_id)){//如果有缓存的 标准耗材
            return Cache::get('supply_item_'.$item_id);
        }else{
            $si_arr=SupplyItem::leftJoin("anhui_data.supply_series","anhui_data.supply_series.id","=","supply_item.series_id")
            ->leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")
            ->leftJoin("anhui_data.supply_unit as bz_unit","bz_unit.id","=","supply_item.unit_id")
            ->leftJoin("anhui_data.supply_unit as sale_unit","sale_unit.id","=","supply_item.sale_unit_id")
            // ->leftJoin(DB::raw("(select * from anhui.supply_sub where `status`=1) as supply_sub"), 'supply_sub.supply_id',"=","supply_item.id")
            ->leftJoin("supply_classification","supply_classification.id","=","supply_item.supply_classification_id")->
            select("supply_item.id","supply_item.code","supply_item.old_code","supply_item.supply_classification_id","supply_item.supply_classification_id as subject","supply_classification.name as subject_name","supply_item.brand_id","supply_brand.brand_name","supply_item.series_id","supply_series.name as series_name","sale_unit.name as unit","supply_item.model","bz_unit.name as bz_unit","supply_item.volume_size as pack","supply_item.status","supply_item.car_model_comments","supply_item.supply_classification_id as st_classifi_id","supply_classification.name as scf_name","supply_item.name","supply_item.type")->where("supply_item.id",$item_id)->first();
            $ssub=SupplySub::where("supply_id",$item_id)->where("status",1)->first();
            if($si_arr){
                $si_arr["specification"]="";
                $si_arr["amount"]=isset($ssub->usual_price)?$ssub->usual_price:0;
                Cache::add('supply_item_'.$item_id,$si_arr, 10);
            }
            return $si_arr;
        }

    }

    /**
     * @Author:      lyt
     * @DateTime:    2015-10-08 15:16:14
     * @Description: 参数的数组 用于保存一个耗材信息
     */
    public static function saveByDate($date){
        $si=new SupplyItem();
        $si->subject=$date['subject'];
        $si->brand_id=$date['brand_id'];
        $si->series_id=$date['series_id'];
        $si->model=$date['model'];
        $si->status=$date['status'];
        $si->icon=$date['icon'];
        $si->amount=$date['amount'];
        $si->purchase_price=$date['purchase_price'];
        $si->desc=$date['desc'];
        $si->small_desc=$date['small_desc'];
        $si->save();
        return $si;
    }
    
    public static function getThreeClassification($list_id){
        $scf_one=InventoryClassification::where('list_id',$list_id)->where('level',1)->select('scf_id')->get()->toArray();
        $scf_one=array_column($scf_one,'scf_id');
        $one_two=SupplyClassification::where('level',2)->whereIn('parent_id',$scf_one)->select('id')->get()->toArray();
        $scf_two=array_column($one_two,'id');
        $one_three=SupplyClassification::where('level',3)->whereIn('parent_id',$scf_two)->select('id')->get()->toArray();
        //根据第二份额里获取三级分类
        $scf_two=InventoryClassification::where('list_id',Input::get('list_id'))->where('level',2)->select('scf_id')->get()->toArray();
        $scf_two=array_column($scf_two,'scf_id');
        $two_three=SupplyClassification::where('level',3)->whereIn('parent_id',$scf_two)->select('id')->get()->toArray();
        //根据第二份额里获取三级分类
        $three_three=InventoryClassification::leftJoin('supply_classification as sf3','sf3.id','=','inventory_classification.scf_id')
        ->where('inventory_classification.list_id',Input::get('list_id'))
        ->where('inventory_classification.level',3)
        ->select('sf3.id')
        ->get()
        ->toArray();
        $three=array();
        foreach($one_three as $_one_three){
            $three[]=$_one_three['id'];
        }
        foreach($two_three as $_two_three){
            $three[]=$_two_three['id'];
        }
        foreach($three_three as $_three_three){
            $three[]=$_three_three['id'];
        }
        return $three;
    }
}
